package show.me.the.money.accredit.condition;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Element;

public class ConditionFactory {
	private static Log logger = LogFactory.getLog(ConditionFactory.class);
	private static HashMap<String,String> cls = new HashMap<String,String>();
	static{
		cls.put("exp", "ctd.accredit.condition.JsonExpCondition");
		cls.put("filter", "ctd.accredit.condition.JsonExpCondition");
		cls.put("notify", "ctd.accredit.condition.JsonExpCondition");
		cls.put("override", "ctd.accredit.condition.OverrideCondition");
		cls.put("limit", "ctd.accredit.condition.LimitCondition");
	}	
	public static Condition createCondition(Element define){
		if(define == null){
			return null;
		}
		Condition cnd;
		try {
			cnd = (Condition)Class.forName(cls.get(define.attributeValue("type","exp"))).newInstance();
			cnd.setDefine(define);
			return cnd;
		} 
		catch (Exception e) {
			logger.error("condition init failed:", e);
		}
		return null;
	} 
}
